#!/usr/bin/python
# -*- coding: UTF-8 -*-

import shutil
import os
from Scripts.Common import EnumOutput
from Scripts.Output import OutputJsonProcess
from Scripts.Output import OutputXmlProcess


#
# it deletes all of files in the target folders, creates data files, and outputs the config data
# @param [outputType] the output file format
# @param [outputFolderUrls] all of target folder urls
# @param [excelConfigDatas] all of the config contents
#
def Run(outputType, outputFolderUrls, excelConfigDatas):
    #deletes all of files in the target folders
    outputFolderCount = len(outputFolderUrls)
    for i in range(outputFolderCount):
        outputFolderUrl = outputFolderUrls[i]
        _DoDeleteAllFiles(outputFolderUrl)
        _DoCreateAllFolders(outputFolderUrl)

    #creates all of data files
    if EnumOutput.JSON == outputType:
        OutputJsonProcess.Run(outputFolderUrls, excelConfigDatas)
    elif EnumOutput.XML == outputType:
        OutputXmlProcess.Run(outputFolderUrls, excelConfigDatas)
    
#
# it deletes all of files in a folder
# @param [folderUrl] the folder url
#
def _DoDeleteAllFiles(folderUrl):
    try:
        shutil.rmtree(folderUrl)
    except:
        return

#
# it creates all of folders in a url
# @param [folderUrl] the folder url
#
def _DoCreateAllFolders(folderUrl):
    try:
        os.makedirs(folderUrl)
    except:
        return